﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Modules.stats.experiments.*;

    public class ExpansionManager {

        private static var m_instance:ExpansionManager;

        public static function get instance():ExpansionManager{
            if (!m_instance){
                m_instance = new (ExpansionManager)();
            };
            return (m_instance);
        }

        public function getExpansionDataByLevel(_arg1:int):Object{
            var xml:* = null;
            var expansions:* = null;
            var searchNum:* = null;
            var expansionData:* = null;
            var attribute:* = null;
            var level:* = _arg1;
            xml = Global.gameSettings.getXML();
            expansions = xml.expansions.expansion;
            searchNum = ((level < 10)) ? ("0" + level) : level.toString();
            var desiredExpansion:* = expansions.(attribute("num") == searchNum)[0];
            if (!desiredExpansion){
                desiredExpansion = expansions.(attribute("num") == "MAX")[0];
            };
            expansionData = {};
            for each (attribute in desiredExpansion.attributes()) {
                expansionData[attribute.name().localName] = attribute.toXMLString();
            };
            return (expansionData);
        }
        public function getBehaviorExperimentVariant():int{
            return (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_NEW_EXPANSION_BEHAVIOR));
        }
        public function getNextExpansionLevelRequirement():int{
            var _local1:Object = this.getExpansionDataByLevel((Global.player.expansionsPurchased + 1));
            return (parseInt(_local1["level"]));
        }
        public function getNextExpansionPermitRequirement():int{
            var _local1:Object = this.getExpansionDataByLevel((Global.player.expansionsPurchased + 1));
            var _local2:int;
            switch (this.getBehaviorExperimentVariant()){
                case ExperimentDefinitions.NEW_EXPANSION_BEHAVIOR_ACTIVATED:
                    _local2 = parseInt(_local1["permits2"]);
                    break;
                default:
                    _local2 = parseInt(_local1["permits"]);
            };
            return (_local2);
        }
        public function getNextExpansionNumPermitPurchaseToComplete():int{
            var _local1:int = this.getNextExpansionPermitRequirement();
            var _local2:int = _local1;
            switch (this.getBehaviorExperimentVariant()){
                case ExperimentDefinitions.NEW_EXPANSION_BEHAVIOR_ACTIVATED:
                    _local2 = Math.max(0, (_local1 - Global.player.inventory.getItemCountByName(Item.PERMIT_ITEM)));
                    break;
                default:
                    _local2 = _local1;
            };
            return (_local2);
        }
        public function getNextExpansionPermitCostToComplete():int{
            var _local1:int;
            switch (this.getBehaviorExperimentVariant()){
                case ExperimentDefinitions.NEW_EXPANSION_BEHAVIOR_ACTIVATED:
                    _local1 = (this.getNextExpansionNumPermitPurchaseToComplete() * Global.gameSettings.getItemByName(Item.PERMIT_ITEM).cash);
                    break;
                default:
                    _local1 = this.getNextExpansionLockOverrideCost();
            };
            return (_local1);
        }
        public function getNextExpansionPopulationRequirement():int{
            var _local3:int;
            var _local4:Object;
            var _local1:Object = this.getExpansionDataByLevel((Global.player.expansionsPurchased + 1));
            var _local2:int;
            switch (this.getBehaviorExperimentVariant()){
                case ExperimentDefinitions.NEW_EXPANSION_BEHAVIOR_ACTIVATED:
                    _local2 = parseInt(_local1["population"]);
                    if (Global.player.expansionsPurchased > 0){
                        _local3 = (Global.player.populationObservedOnLastExpansion + parseInt(_local1["popIncrement"]));
                        _local4 = this.getExpansionDataByLevel(Global.player.expansionsPurchased);
                        if ((((_local3 < _local2)) && ((Global.player.populationObservedOnLastExpansion < parseInt(_local4["population"]))))){
                            _local2 = _local3;
                        };
                    };
                    break;
                default:
                    _local2 = 0;
            };
            return (_local2);
        }
        public function getNextExpansionPurchaseMultiplier():Number{
            var _local2:Object;
            var _local1:Number = 1;
            switch (this.getBehaviorExperimentVariant()){
                case ExperimentDefinitions.NEW_EXPANSION_BEHAVIOR_ACTIVATED:
                    _local1 = 1;
                    break;
                default:
                    _local2 = this.getExpansionDataByLevel(Global.player.expansionCostLevel);
                    _local1 = parseFloat(_local2["goldMultiplier"]);
            };
            return (_local1);
        }
        public function getNextExpansionLockOverrideCost():int{
            var _local1:Object = this.getExpansionDataByLevel((Global.player.expansionsPurchased + 1));
            var _local2:int;
            switch (this.getBehaviorExperimentVariant()){
                case ExperimentDefinitions.NEW_EXPANSION_BEHAVIOR_ACTIVATED:
                    _local2 = parseInt(_local1["cost2"]);
                    break;
                default:
                    _local2 = parseInt(_local1["cost"]);
            };
            return (_local2);
        }
        public function hasEnoughPermits():Boolean{
            var _local1:int = this.getNextExpansionPermitRequirement();
            var _local2:int = Global.player.inventory.getItemCountByName(Item.PERMIT_ITEM);
            return ((_local2 >= _local1));
        }
        public function hasEnoughPopulation():Boolean{
            return (Global.player.checkPopulation(this.getNextExpansionPopulationRequirement()));
        }
        public function hasPassedExpansionGate():Boolean{
            return (((this.hasEnoughPopulation()) && (this.hasEnoughPermits())));
        }
        public function getUnlockLabel():String{
            var _local1 = "";
            if (!this.hasEnoughPermits()){
                _local1 = ZLoc.t("Dialogs", "permit_text", {amount2:this.getNextExpansionPermitRequirement()});
            } else {
                if (!this.hasEnoughPopulation()){
                    _local1 = ZLoc.t("Dialogs", "population_text1", {amount2:(this.getNextExpansionPopulationRequirement() * Global.gameSettings.getNumber("populationScale", 1))});
                };
            };
            return (_local1);
        }

    }
}//package Classes 
